var express = require('express');
var router = express.Router();
var fs = require("fs");
var Db = require("../database");
var config = require("../config");
var utils = require('../utils');
var logger = require("../logger");

function notice(req,res){
    var page = req.body.page || 1;
    var size = req.body.size || 20;
    page = parseInt(page);
    size = parseInt(size);

    var start = req.body.start;
    var end = req.body.end;
    if(!start){
        start = new Date();
    }
    var thisDate = utils.getDate(start,end);
    var startDate = thisDate.start;
    var endDate = thisDate.end;


    if(page < 1 || isNaN(page)){
        page = 1;
    }
    if(size < 10 || isNaN(size)){
        size = 20;
    }
    var startPage = (page - 1) * size;

    Db.getConnection(function(err,conn){
        if(err){
            res.json({status:0,err:err.message});
        }else{
            conn.collection(Db.noticeCollection).count(function(err,count){
                if(err){
                    res.json({status:0,err:err.message});
                }else{
                    conn.collection(Db.noticeCollection).find({skip:startPage,limit:size,sort:{data:-1}}).toArray(function(err,listInfo){
                        if(err){
                            res.json({status:0,error:err.message});
                        }else{
                            utils.formatDate(listInfo);
                            res.json({status:1,error:"",data:{list:listInfo,total:count,page:page}});
                        }
                    })
                }
            })
        }
    })
}

function talk(req,res){
    var page = req.body.page || 1;
    var size = req.body.size || 20;
    page = parseInt(page);
    size = parseInt(size);

    var session = req.cookies[config.sessionName];

    var start = req.body.start;
    var end = req.body.end;
    if(!start){
        start = new Date();
    }
    var thisDate = utils.getDate(start,end);
    var startDate = thisDate.start;
    var endDate = thisDate.end;


    if(page < 1 || isNaN(page)){
        page = 1;
    }
    if(size < 10 || isNaN(size)){
        size = 20;
    }
    var startPage = (page - 1) * size;

    Db.getConnection(function(err,conn){
        if(err){
            res.json({status:0,err:err.message});
        }else{
            conn.collection(Db.talkCollection).count(function(err,count){
                if(err){
                    res.json({status:0,err:err.message});
                }else{
                    conn.collection(Db.talkCollection).find({skip:startPage,limit:size,sort:{data:-1}}).toArray(function(err,listInfo){
                        if(err){
                            res.json({status:0,error:err.message});
                        }else{
                            utils.formatDate(listInfo);
                            res.json({status:1,error:"",data:{list:listInfo,total:count,page:page}});
                        }
                    })
                }
            })
        }
    })
}

router.post("/notice",notice);
router.post("/talk",talk);

module.exports = router;